import java.util.ArrayList;
import java.util.Calendar;

/**
 * @projectName: LeetCode
 * @package: PACKAGE_NAME
 * @className: Point6
 * @author: Mocrash
 * @description: TODO
 * @date: 2024/10/18 12:19
 * @version: 1.0
 */
public class Point6 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String paypalishiring = solution.convert("PAYPALISHIRING", 4);
        System.out.println(paypalishiring);
    }

    private static class Solution {
        public String convert(String s, int numRows) {
            if (numRows == 1)return s;
            ArrayList<ArrayList<Character>> map = new ArrayList<>(numRows);
            for (int i = 0; i < numRows; ++i){
                map.add(new ArrayList<>());
            }
            int moveNum = 1, index = 0;
            for (int i = 0; i < s.length(); i++) {
                map.get(index).add(s.charAt(i));
                if (index == numRows - 1){
                    moveNum = -1;
                }
                if (index == 0){
                    moveNum = 1;
                }
                index += moveNum;
            }
            char[] retArr = new char[s.length()];
            index = 0;
            for (int i = 0; i < numRows; i++) {
                ArrayList<Character> characters = map.get(i);
                for (int j = 0; j < characters.size(); j++) {
                    retArr[index++] = characters.get(j);
                }
            }
            return String.valueOf(retArr);
        }
    }
}
